<?php

class View_Helper_ImageHelper {

    function image($width, $height, $zoom, $quality, $sourceFilename) {
        include_once('phpThumb/phpthumb.class.php');
        error_reporting(E_WARNING);
        ini_set('display_errors', '1');

        $phpThumb = new phpThumb();
        $phpThumb->src = $sourceFilename;
        if ($width != null)
            $phpThumb->w = $width;
        if ($height != null)
            $phpThumb->h = $height;
        if ($zoom != null)
            $phpThumb->zc = $zoom;
        if ($quality != null)
            $phpThumb->q = $quality;
        $phpThumb->config_imagemagick_path = '/usr/bin/convert';
        $phpThumb->config_prefer_imagemagick = false;
        $phpThumb->config_output_format = 'jpg';
        $phpThumb->config_error_die_on_error = true;
        $phpThumb->config_document_root = '';
        $phpThumb->config_temp_directory = './temp/';
        $phpThumb->config_cache_directory = './cache/';
        $phpThumb->config_cache_disable_warning = true;
        $cacheFilename = md5($phpThumb->src);
        $phpThumb->cache_filename = $phpThumb->config_cache_directory . $cacheFilename . 'w' . $width . 'h' . $height . 'z' . $zoom . 'q' . $quality;

        if (!is_file($phpThumb->cache_filename)) {
            if (@getimagesize($sourceFilename)) {
                if ($phpThumb->GenerateThumbnail()) {
                    $phpThumb->RenderToFile($phpThumb->cache_filename);
                } else {
                    die('Failed: ' . $phpThumb->error);
                }
            } else { // Can't read source
                die("Couldn't read source image " . $sourceFilename);
            }
        }


        if (is_file($phpThumb->cache_filename)) { // If thumb was already generated we want to use cached version
            $cachedImage = getimagesize($phpThumb->cache_filename);
            //header('Content-Type: ' . $cachedImage['mime']);
            readfile($phpThumb->cache_filename);
            exit;
        } else {
            readfile($sourceFilename);
            exit;
        }
    }

    function graphics() {
        //Metemos la grafica con puro php gif BAR graph
        $GraphWidth = 400;
        $GraphHeight = 200;
        $GraphFont = 2;
        $GraphData = array("99", "75", "15","66","22");
        $GraphLabel = array("Beef", "chicken","pork", "lamb", "lobster");

        //creando la imagen
        $image = imagecreate($GraphWidth, $GraphHeight);
        
//        var_dump($image);
//        die;
        //allocate colors
        $colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
        $colorGrid = imagecolorallocate($image, 0xCC, 0xCC, 0xCC);
        $colorBar = imagecolorallocate($image, 0xFF, 0x00, 0x00);
        $colorText = imagecolorallocate($image, 0x00, 0x00, 0x00);

        //fill bacground
        imagefill($image, 0, 0, $colorBody);

        //draw vertical grid line
        $GridLabelWidth = imagefontwidth(($GraphFont) * 3 + 1);
        imageline($image, $GridLabelWidth, 0, $GridLabelWidth, $GraphHeight - 1, $colorGrid);

        //draw horizontal grid lines 
        $GraphScale = 2;
        for ($index = 0; $index < $GraphHeight; $index += $GraphHeight / 10) {
            imagedashedline($image, 0, $index, $GraphWidth - 1, $index, $colorGrid);
            //draw label
            imagestring($image, $GraphFont, 0, $index, round(($GraphHeight - $index) / $GraphScale), $colorText);
        }

        //add botton line
        imageline($image, 0, $GraphHeight - 1, $GraphWidth - 1, $GraphHeight - 1, $colorGrid);

        //draw each bar
        $BarWidth = (($GraphWidth - $GridLabelWidth) / count($GraphData)) - 10;

        for ($index = 0; $index < count($GraphData); $index++) 
        {
            //draw bar
            $BarTopX = $GridLabelWidth + (($index + 1) * 10) + ($index * $BarWidth);
            $BarBottomX = $BarTopX + $BarWidth;
            $BarBottomY = $GraphHeight - 1;
            $BarTopY = $BarBottomY - ($GraphData[$index] * $GraphScale);
            imagefilledrectangle($image, $BarTopX, $BarTopY, $BarBottomX, $BarBottomY, $colorBar);

            //draw label
            $labelX = $BarTopX + (($BarBottomX - $BarTopX) / 2) - (imagefontheight($GraphFont) / 2);
            $labelY = $BarTopY - 10;

            imagestringup($image, $GraphFont, $labelX, $labelY, "Grafica1", $colorText);
        }

        // salida para un img 
//            imagettftext($image, 50, 0, 0, 0, $colorBar, $GraphFont, "Graphic");
        //output image
//        header("Content-type: image/gif");
        imagegif($image);
//            imagedestroy($image);
//            return ($image);
    }

    
    function imgDinamic() 
    {
        $ancho = 100;
        $alto = 30;
        $imagen = imageCreate($ancho, $alto);
        $amarillo = ImageColorAllocate($imagen, 255, 255, 0);
        ImageFill($imagen, 0, 0, $amarillo);
        $rojo = ImageColorAllocate($imagen, 255, 0, 0);
        $valoraleatorio = rand(100000, 999999);
        ImageString($imagen, 5, 25, 5, $valoraleatorio, $rojo);
        for ($c = 0; $c <= 5; $c++) 
        {
            $x1 = rand(0, $ancho);
            $y1 = rand(0, $alto);
            $x2 = rand(0, $ancho);
            $y2 = rand(0, $alto);
            ImageLine($imagen, $x1, $y1, $x2, $y2, $rojo);
        }
        Header("Content-type: image/jpeg");
        imagejpeg($imagen);
        ImageDestroy($imagen);
    }

}
